5 Dev Tricks I Learned This Week

1. Searching and replacing in vi

 

I hate vi. It’s my least favorite editor, except for maybe something like Ed, but I don’t know anyone who solely uses Ed and loves to talk about how great it is. Yet vi is default on many linux systems, and not every situation provides the opportunity to install an alternate editor, so sometimes you’re stuck.

In those cases, it’s good to know what to do. There are plenty of resources on vi, and most of the commands are fairly straightforward if somewhat arbitrary at first.

 

Searching and replacing text is just under the threshold of something you do every day, but common enough that you want to know how to do it.

Well, it’s pretty simple:

 

Search — :s/<string>

 

Replace — :s/<pattern>/<replace>/

 

Pretty straightforward. There’s a lot more to it, but that’s enough for many common applications.

 

2. Apply specific files from Git’s stash

 

Git’s stash command is highly useful when you’re moving around in code and codebases, and it’s great at what it does. In its most simple form, though, it only holds or applies the entire chunk of your work. What if you end up with more than one chunk of work held by the same stash? Well, you run this:

 

git checkout stash@{0} -- <filename>

 

and replace the zero with whichever stash you are pulling from.

See more here.

 

3. Doing a Ruby whitespace “squish” without Rails

 

Rails’ “squish” method is a like the similar trim command in many languages, only it also trims down the whitespace in the middle of the string, leaving a nice, clean bit of text.

You can replicate this pretty quickly, by running this on the string you need condensed:

 

stringvar.split.join(" ")

 

4. Getting an attribute’s value with Nokogiri

 

Nokogiri is a Ruby library with tools for parsing, searching, and manipulating HTML and XML. It’s dead handy, and for basic applications, dead simple, but like many aspects of Ruby, can be tricky once you get a little in-depth.

 

Turns out there are a couple ways to get an attribute’s value, each with its merits. I’ll let the original discussion do its own talking on this one: http://stackoverflow.com/questions/7107642/getting-attributes-value-in-nokogiri-to-extract-link-urls

 

5. Performing a diff online

Diff is a great tool for comparing text. But if the text is not on the same system as your linux build, or if you want something a bit more visual and easy to manipulate, using an online tool is a fantastic way to speed up the process — it certainly helped me.

DiffNow is the service I use the most. It provides several options for input, and is very clean and easy to use.

 


 

Well, that’s it for now. I’m sure there are other tips I picked up, but these are the ones I remembered to bookmark!

 

I’ll leave you with a small bonus: a very old chain of jokes about Ed, the editor I mentioned earlier.

https://www.gnu.org/fun/jokes/ed-msg.html

Does it get geekier?

 

Thanks for reading!

 

Leave a Reply

Your email address will not be published. Required fields are marked *